package com.minyifang.wcspf.work.ui.home.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.minyifang.wcspf.R;
import com.minyifang.wcspf.common.utils.ImageUtils;
import com.minyifang.wcspf.work.ui.goods.activities.GoodsDetailsActivity;
import com.minyifang.wcspf.work.ui.home.model.GoodsBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by LuoYaWei on 2018/7/12.
 */
public class CommonGoodsAdapter extends RecyclerView.Adapter<CommonGoodsAdapter.Holder> {

    private Context mContext;
    private List<GoodsBean> mBeanList;

    public CommonGoodsAdapter(Context context, List<GoodsBean> list) {
        this.mContext = context;
        this.mBeanList = list;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        return new Holder(LayoutInflater.from(mContext).inflate(R.layout.item_recommend_goods_list, parent, false));
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull CommonGoodsAdapter.Holder holder, int position) {

        GoodsBean goodsBean = mBeanList.get(position);
        if (goodsBean.getIsNew() == 1)
            holder.item_isNew.setVisibility(View.VISIBLE);
        else
            holder.item_isNew.setVisibility(View.GONE);

        ImageUtils.getInstance().showImage( goodsBean.getDeputyPic(), holder.item_pic);
        holder.item_name.setText(goodsBean.getName());
        holder.item_original_price.setText(goodsBean.getMarketPrice());
        holder.item_original_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        holder.item_discount_price.setText(String.format("￥%s", goodsBean.getUnitPrice()));
        holder.item_unit.setText("/" + goodsBean.getPriceName());


        holder.itemView.setOnClickListener(v -> GoodsDetailsActivity.start(mContext, goodsBean.getID()));
    }

    @Override
    public int getItemCount() {
        return mBeanList.size();
    }

    public class Holder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_isNew)
        FrameLayout item_isNew;
        @BindView(R.id.item_pic)
        ImageView item_pic;
        @BindView(R.id.item_name)
        TextView item_name;
        @BindView(R.id.item_original_price)
        TextView item_original_price;
        @BindView(R.id.item_discount_price)
        TextView item_discount_price;
        @BindView(R.id.item_unit)
        TextView item_unit;

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
